package cn.iocoder.yudao.module.signature.controller.admin.application;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoPageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.application.vo.ApplicationInfoSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.application.ApplicationInfoDO;
import cn.iocoder.yudao.module.signature.service.application.ApplicationInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 应用")
@RestController
@RequestMapping("/signature/application-info")
@Validated
public class ApplicationInfoController {

    @Resource
    private ApplicationInfoService applicationInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建应用")
    @PreAuthorize("@ss.hasPermission('signature:application-info:create')")
    public CommonResult<Long> createApplicationInfo(@Valid @RequestBody ApplicationInfoSaveReqVO createReqVO) {
        return success(applicationInfoService.createApplicationInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新应用")
    @PreAuthorize("@ss.hasPermission('signature:application-info:update')")
    public CommonResult<Boolean> updateApplicationInfo(@Valid @RequestBody ApplicationInfoSaveReqVO updateReqVO) {
        applicationInfoService.updateApplicationInfo(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除应用")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('signature:application-info:delete')")
//    public CommonResult<Boolean> deleteApplicationInfo(@RequestParam("id") Long id) {
//        applicationInfoService.deleteApplicationInfo(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得应用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('signature:application-info:query')")
    public CommonResult<ApplicationInfoRespVO> getApplicationInfo(@RequestParam("id") Long id) {
        ApplicationInfoDO applicationInfo = applicationInfoService.getApplicationInfo(id);
        return success(BeanUtils.toBean(applicationInfo, ApplicationInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应用分页")
    @PreAuthorize("@ss.hasPermission('signature:application-info:query')")
    public CommonResult<PageResult<ApplicationInfoRespVO>> getApplicationInfoPage(@Valid ApplicationInfoPageReqVO pageReqVO) {
        PageResult<ApplicationInfoDO> pageResult = applicationInfoService.getApplicationInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出应用 Excel")
    @PreAuthorize("@ss.hasPermission('signature:application-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApplicationInfoExcel(@Valid ApplicationInfoPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApplicationInfoDO> list = applicationInfoService.getApplicationInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "应用.xls", "数据", ApplicationInfoRespVO.class,
                BeanUtils.toBean(list, ApplicationInfoRespVO.class));
    }

    @PostMapping("/ipa-info-extractor")
    @Operation(summary = "获取应用 IPA 包信息")
    @PreAuthorize("@ss.hasPermission('signature:application-info:ipa-info-extractor')")
    public CommonResult<?> getIpaInfoExtractor(@RequestParam("file") MultipartFile file) {
        return success(applicationInfoService.getIpaInfoExtractor(file));
    }


    @PostMapping("/delete")
    @Operation(summary = "删除数据（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:application-info:delete')")
    public CommonResult<Boolean> delete(@RequestBody List<Long> ids) throws Exception {
        applicationInfoService.delete(ids,null);
        return success(Boolean.TRUE);
    }

    @PostMapping("/enable")
    @Operation(summary = "启用（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:application-info:enable')")
    public CommonResult<Boolean> enable(@RequestBody List<Long> ids) {
        applicationInfoService.enable(ids);
        return success(Boolean.TRUE);
    }

    @PostMapping("/disable")
    @Operation(summary = "禁用（单个或者批量）")
    @PreAuthorize("@ss.hasPermission('signature:application-info:disable')")
    public CommonResult<Boolean> disable(@RequestBody List<Long> ids) {
        applicationInfoService.disable(ids);
        return success(Boolean.TRUE);
    }
}